//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//
// --------------------------------------------------------------------
//                                                                     
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A      
// PARTICULAR PURPOSE.                                                 
// Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//                                                                     
// --------------------------------------------------------------------

#include <WINDOWS.H>
#include <types.h>
#include <ceddk.h>
#include <ceotgbus.h>
#define __THIS_FILE__ TEXT("OtgTest.cpp")

#define COMM_ERROR( P, T, C ); \
    if( T ) { \
        NKDbgPrintfW (TEXT("FAIL in %s @ line %d:  GetLastError() = %d"), \
                      __THIS_FILE__, __LINE__, GetLastError() ); \
        C; \
    }
LPCTSTR JumpToNumSpace(LPCTSTR lpStr)
{
    while (lpStr && (*lpStr==_T(' ') || *lpStr==_T('\t')))
        lpStr ++ ;
    return lpStr;
}
LPCTSTR  GetWord(LPCTSTR lpSource, LPTSTR lpDest,DWORD dwDestSize)
{
    if (lpSource && lpDest && dwDestSize) {
        while (dwDestSize>1 && (_istalpha(*lpSource) || _istalnum(*lpSource))) {
            *(lpDest++) = *(lpSource++);
            dwDestSize -- ;
        }
        *lpDest = 0 ;
    }
    return lpSource;
}
BOOL IsString(LPCTSTR lpStr, LPCTSTR lpTarget)
{
    if (lpStr && *lpStr &&lpTarget) {
        lpStr = JumpToNumSpace(lpStr);
        TCHAR tString[MAX_PATH];
        tString[0] = 0 ;
        lpStr = GetWord(lpStr,tString,MAX_PATH);
        if (!_tcsicmp(tString,lpTarget)) {
            return TRUE;
        }
        
    }
    return FALSE;     
}
BOOL OtgSet(HANDLE hOTG, LPCTSTR lpCmdLine)
{
    
    if (lpCmdLine && *lpCmdLine!=0) {
        lpCmdLine = JumpToNumSpace(lpCmdLine);
        TCHAR tString[MAX_PATH] ;
        tString [ 0 ] = 0;
        if (lpCmdLine && *lpCmdLine!=0) {
            lpCmdLine = GetWord(lpCmdLine,tString,MAX_PATH);
            if (!_tcsicmp(tString,TEXT("BusRequest"))) {
                DWORD dwRequest = 1;
                if ( IsString(lpCmdLine,TEXT("reset"))) {
                    dwRequest = 0;
                }
                return DeviceIoControl(hOTG,IOCTL_BUS_USBOTG_REQUEST_BUS,&dwRequest,sizeof(dwRequest),NULL, 0,   NULL,NULL);                    
            }
            else if (!_tcsicmp(tString,TEXT("BusDrop"))) {
                DWORD dwRequest = 1;
                if ( IsString(lpCmdLine,TEXT("reset"))) {
                    dwRequest = 0;
                }
                return DeviceIoControl(hOTG,IOCTL_BUS_USBOTG_DROP_BUS,&dwRequest,sizeof(dwRequest),NULL, 0,   NULL,NULL);                    
            }
        }
    }
    return FALSE;
}
#define OTG_DEVICE_NAME TEXT("OTG1:")
// --------------------------------------------------------------------
INT WINAPI 
WinMain(
    HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, 
    LPWSTR lpCmdLine,	
    INT nCmdShow)
// --------------------------------------------------------------------    
{   
    HANDLE hOTG = CreateFile(OTG_DEVICE_NAME,GENERIC_READ|GENERIC_WRITE,0, NULL,OPEN_EXISTING,0,NULL);
    BOOL bSuccess = FALSE;
    if ( hOTG!=INVALID_HANDLE_VALUE ) {
    // run test and log results
        if(OtgSet(hOTG , lpCmdLine)) {
            bSuccess = TRUE;
        }
        CloseHandle(hOTG);
    }
    if (bSuccess) {
        NKDbgPrintfW(L"OtgSet Test Passed\n");
        NKDbgPrintfW(L"@@@@@@0\n");
    }
    else {
        NKDbgPrintfW(L"OtgSet Test Failed, GetLastError=%d\n",GetLastError());
        NKDbgPrintfW(L"@@@@@@1\n");
    }
    
    return 1;
}

